package com.jiliason.testTools;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;


public class JsonT {
    private static final ObjectMapper JsonMapper =
            // 没有对应字段不抛错
            new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
                    // java对象时忽略null的字段
                    .setSerializationInclusion(JsonInclude.Include.NON_NULL);

    public static String pojoToJson(Object pojo) {
        try {
            return JsonMapper.writeValueAsString(pojo);
        } catch (JsonProcessingException ignored) {}
        return null;
    }

    public static <T> T jsonToPojo(String json, Class<T> clazz) {
        try {
            return JsonMapper.readValue(json, clazz);
        } catch (JsonProcessingException ignored) {}
        return null;
    }

}
